 

// 1.算法题：https://leetcode-cn.com/problems/linked-list-cycle-ii/


/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
 var detectCycle = function(head) {
    let set= new Set();
    let res=null;
    let temp = head;
    while(temp){
        if(set.has(temp)){
            res = temp
            break;
        }else{
            set.add(temp)
        }
        temp=temp.next
    }
    return res;
};


// 2.手写题：https://bigfrontend.dev/zh/problem/implement-Promise-any



/**
 * @param {Array<Promise>} promises
 * @return {Promise}
 */
 function any(promises) {
    // your code here
    if (!promises.length) {
      throw new AggregateError("No Promise");
    }
    return new Promise((resolve, reject) => {
      let num = promises.length, errors = []
      for (let i = 0; i < num; i++) {
        promises[i]
          .then(res => resolve(res))
          .catch(error => {
            errors[i] = error;
            if (errors.length === num) {
              reject(new AggregateError(
                'No Promise in Promise.any was resolved',
                errors
              ))
            }
          })
      }
  
    })
  }

